#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: parseHomePage
# 爬取首页

import requests, json, random


# foundTargetDict = []


def extDictFromJson(jsonStr):
    dic = json.loads(jsonStr)
    return dic


def parseDictToAdvSql(name, dic):
    sql = "insert into home_decoration_adv values ('{}','{}','{}',{});".format(name,
                                                                              dic['PICTURE_ADDRESS'],
                                                                              dic['TO_PLACE'],
                                                                              dic['urlType'],
                                                                              )
    return sql


def parseDictToSlideAndFloor(name, dic, loc_no):
    sql = "insert into goods_top values ('{}','{}','{}',{},{});".format(
        name,
        dic['goodsId'],
        dic['image'],
        dic.setdefault('urlType', 0),
        loc_no
    )
    return sql


#
# def list_dictionary(d, n_tab=-1, *args, **kwargs):
#     if isinstance(d, list):
#         for i in d:
#             kwargs['source_list'] = list_dictionary(i, n_tab, source_list=kwargs['source_list'])
#     elif isinstance(d, dict):
#         if 'PICTURE_ADDRESS' in d:
#             foundTargetDict.append(d)
#         else:
#             n_tab += 1
#             for key, value in d.items():
#                 tup1 = ()
#                 # print("{}key:{}".format("\t" * n_tab, key))
#                 if key == 'image' or key == 'PICTURE_ADDRESS':
#                     tup1 = tup1 + (key,)
#                 kwargs['source_list'] = list_dictionary(value, n_tab, source_list=kwargs['source_list'], tup1=tup1)
#     else:
#         # print("{}{}".format("\t" * n_tab, d))
#         if len(kwargs['tup1']) > 0:
#             fileprops = extract_image_name(d)
#             if fileprops[0] != '':
#                 kwargs['tup1'] = kwargs['tup1'] + (d,) + fileprops
#                 kwargs['source_list'].append(kwargs['tup1'])
#     return kwargs['source_list']


url = 'https://wxmini.baixingliangfan.cn/baixing/wxmini/homePageContent'
data = {'lon': '115.02932', 'lat': '35.76189'}

r = requests.post(url, data)
# print(r)
print(r.text)
# print(r.content)

adv_sqls = []
homeHead = extDictFromJson(r.text)['data']

print('=================== home_decoration_adv ===================')
# print(homeHead)
# print("integralMallPic={}".format(homeHead['integralMallPic']))
# print("toShareCode={}".format(homeHead['toShareCode']))
# print("advertesPicture={}".format(homeHead['advertesPicture']))
# print("saoma={}".format(homeHead['saoma']))
# print("newUser={}".format(homeHead['newUser']))
# print("floor1Pic={}".format(homeHead['floor1Pic']))
# print("floor2Pic={}".format(homeHead['floor2Pic']))
# print("floor3Pic={}".format(homeHead['floor3Pic']))


adv_sqls.append(parseDictToAdvSql("integralMallPic", homeHead['integralMallPic']))
adv_sqls.append(parseDictToAdvSql("toShareCode", homeHead['toShareCode']))
adv_sqls.append(parseDictToAdvSql("advertesPicture", homeHead['advertesPicture']))
adv_sqls.append(parseDictToAdvSql("saoma", homeHead['saoma']))
adv_sqls.append(parseDictToAdvSql("newUser", homeHead['newUser']))
adv_sqls.append(parseDictToAdvSql("floor1Pic", homeHead['floor1Pic']))
adv_sqls.append(parseDictToAdvSql("floor2Pic", homeHead['floor2Pic']))
adv_sqls.append(parseDictToAdvSql("floor3Pic", homeHead['floor3Pic']))

print(len(adv_sqls))
for adsql in adv_sqls:
    print(adsql)

# slides
# print(homeHead['slides'])
print('=================== slides ===================')
i = 0
for slide in homeHead['slides']:
    print(parseDictToSlideAndFloor('slides', slide, i))
    i += 1
# floor1
print('=================== floor1 ===================')
i = 0
for floor1 in homeHead['floor1']:
    print(parseDictToSlideAndFloor('floor1', floor1, i))
    i += 1
# floor2
print('=================== floor2 ===================')
i = 0
for floor2 in homeHead['floor2']:
    print(parseDictToSlideAndFloor('floor2', floor2, i))
    i += 1

# floor3
print('=================== floor3 ===================')
i = 0
for floor3 in homeHead['floor3']:
    print(parseDictToSlideAndFloor('floor3', floor3, i))
    i += 1
# recommend
print('=================== recommend ===================')
for recommend in homeHead['recommend']:
    print("insert into hot_pot_goods values ('{}','{}',{});".format('default-recommend',
                                                                    recommend['goodsId'],
                                                                    random.randrange(80, 100)))
